package com.xsp.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsp.eduservice.entity.EduCourse;
import com.xsp.eduservice.entity.EduTeacher;
import com.xsp.eduservice.service.EduCourseService;
import com.xsp.eduservice.service.EduTeacherService;
import com.xsp.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 热门课程和名师
 * @author xsp
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/eduservice/index")
public class IndexController {
    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    EduTeacherService eduTeacherService;

    //8个热门课程和4位名师
    @GetMapping("hotList")
    public R getHotCourseAndTeacher(){

        //根据view_count获取8个课程
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("view_count");
        courseQueryWrapper.last("limit 8");
        List<EduCourse> courseList = eduCourseService.list(courseQueryWrapper);
        //根据sort获取4位名师
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByAsc("sort");
        teacherQueryWrapper.last("limit 4");
        List<EduTeacher> teacherList = eduTeacherService.list(teacherQueryWrapper);

        return R.ok().data("courseList",courseList).data("teacherList",teacherList);
    }

}
